package com.iot.gateway.tsl.controller;

import com.iot.gateway.tsl.qo.ServiceQo;
import com.iot.gateway.tsl.service.TslServiceService;
import com.iot.gateway.utils.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物模型管理 - 命令管理
 * 提供设备命令的动态增删改查 REST 接口
 *
 * @author guoxinyu
 * @create 2025/5/27 18:00
 */
@RestController
@RequestMapping("/api/device/commands")
@Tag(name = "物模型管理 - 命令管理")
public class TslCommandController {

    private final TslServiceService tslServiceService;

    public TslCommandController(TslServiceService tslServiceService) {
        this.tslServiceService = tslServiceService;
    }

    @Operation(summary = "新增设备命令", description = "为指定设备添加一个命令")
    @PostMapping("/add/{deviceId}")
    public ApiResult<Void> addCommand(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "命令信息，包含 pid/name/desc/required/callType/inputData/outputData/method", required = true)
            @RequestBody ServiceQo commandQo) {
        tslServiceService.addService(deviceId, commandQo);
        return ApiResult.success();
    }

    @Operation(summary = "更新设备命令", description = "更新指定设备的某个命令")
    @PostMapping("/update/{deviceId}")
    public ApiResult<Void> updateCommand(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "命令信息，包含要更新的 pid 和其他字段", required = true)
            @RequestBody ServiceQo commandQo) {
        tslServiceService.updateService(deviceId, commandQo);
        return ApiResult.success();
    }

    @Operation(summary = "删除设备命令", description = "删除指定设备的某个命令")
    @PostMapping("/del/{deviceId}/{pid}")
    public ApiResult<Void> deleteCommand(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId,
            @Parameter(description = "命令 PID", required = true, example = "rebootDevice")
            @PathVariable("pid") String pid) {
        tslServiceService.deleteService(deviceId, pid);
        return ApiResult.success();
    }

    @Operation(summary = "查询设备命令列表", description = "获取指定设备的所有命令")
    @GetMapping("/select/{deviceId}")
    public ApiResult<List<ServiceQo>> getCommands(
            @Parameter(description = "设备 ID", required = true, example = "device123")
            @PathVariable String deviceId) {
        List<ServiceQo> list = tslServiceService.getServices(deviceId);
        return ApiResult.success(list);
    }
}
